package pl.textureStudio
{
	import actions.ActionsManager;

	import pl.textureStudio.data.DataService;
	import pl.textureStudio.data.IDataService;
	import pl.textureStudio.data.IStorage;
	import pl.textureStudio.data.Storage;
	import pl.textureStudio.model.IStudioModelHandlers;
	import pl.textureStudio.model.StudioModel;
	import pl.textureStudio.view.panel.base.PanelType;
	import pl.textureStudio.view.panel.manager.IPanelManager;
	import pl.textureStudio.view.panel.manager.PanelsManager;
	import pl.textureStudio.view.textureList.ITextureList;
	import pl.textureStudio.view.textureList.TextureList;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;

	/**
	 * @author michalr
	 */
	[SWF(backgroundColor="#515151", frameRate="30", width="800", height="600")]
	public class TextureStudio extends Sprite implements ITextureStudio
	{
		/** laduje dane */
		private var dataService:IDataService;
		/** trzyma dane */
		private var storage:IStorage;
		/** trzyma i zarzadza panelami */
		private var panelManager:IPanelManager;
		/** lista textur*/
		private var texturesList:ITextureList;

		public function TextureStudio()
		{
			storage = new Storage();
			dataService = DataService.factory( storage );
			addEventListener( Event.ADDED_TO_STAGE, onATS );
		}

		private function onATS( event:Event ):void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			texturesList = TextureList.factory( this, storage );
			panelManager = PanelsManager.factory( this );
			dataService.loadData( 'textureStudio/data/texturesData.xml', onDataCompleteHandler );
		}

		private function onDataCompleteHandler():void
		{
			StudioModel.dataLoaded = true;
			StudioModel.getInstance().dataLoadCount = storage.dataLength;
			(StudioModel.getInstance() as IStudioModelHandlers).addShowTexturesListHandler( showTexturesList );
			(StudioModel.getInstance() as IStudioModelHandlers).addEditTextureHandler( editTexture );
			(StudioModel.getInstance() as IStudioModelHandlers).addClosePanelHandler( closePanel );
			(StudioModel.getInstance() as IStudioModelHandlers).addTextureHandler( addTexture );
			showStartScreen();
		}

		private function delayedMethod( zmienna1:String, zmienna2:uint ):void
		{
			trace( 'delayed method', zmienna1, zmienna2 );
		}

		private function showStartScreen():void
		{
			ActionsManager.addAction( panelManager.showPanel, 'welcome' ).params( PanelType.WELCOME ).iap( panelManager ).fin();
			ActionsManager.runStandaloneActionByID( 'welcome' );
		}

		private function showTexturesList():void
		{
			ActionsManager.addAction( panelManager.hideCurrentPanel, 'hide' ).chainID( 'texturesList' ).iap( panelManager ).fin();
			ActionsManager.addAction( doShowTexturesList, 'showList' ).chainID( 'texturesList' ).fin();
			ActionsManager.startChainByID( 'texturesList' );
		}

		private function doShowTexturesList():void
		{
			texturesList.show();
		}

		private function closePanel():void
		{
			ActionsManager.addAction( panelManager.hideCurrentPanel, 'closePanel' ).chainID( 'closePanel' ).iap( panelManager ).fin();
			ActionsManager.startChainByID( 'closePanel' );
		}

		private function addTexture():void
		{
			ActionsManager.addAction(panelManager.showPanel, 'showAddText').chainID('newTexture').params(PanelType.CREATE_NEW).iap( panelManager ).fin();
			ActionsManager.startChainByID('newTexture');
		}


		public function editTexture( textureID:String ):void
		{
			StudioModel.getInstance().currentEditedTexture = storage.getTextureByID( textureID );
			ActionsManager.addAction( panelManager.showPanel, 'showPanel' ).chainID( 'showEdit' ).params( PanelType.EDIT ).iap( panelManager ).fin();
			ActionsManager.startChainByID( 'showEdit' );
		}
	}
}
